#!/bin/bash
######################################################################################################################
# 功能:     此脚本用来巡检 DataEase 系统状态及使用情况
# 适用系统: CentOS 7 / RHEL 7 
# 适用 DataEase 模式: LOCAL 模式
# 更新:
#		2023-03-27	fanym	初次提交
#		2023-03-29	fanym	增加检测项：异常数据源、组织数量、用户来源、定时同步失败任务、定时报告失败任务、近 30 日动态。
#		2023-04-10	fanym	增加检测项：视图各类型数量汇总、Doris 最大表数据量。
#		2023-04-13	fanym	优化：Doris、MySQL 的配置从配置文件里读取; 增加检测项：各服务运行时长、部署模式、是否正常备份。
#		2023-04-14	fanym	增加执行进度提示。
#		2023-04-17	fanym	检测组件是否外置部署。
######################################################################################################################


function get_OS_Version(){
	echo ""
	echo "######  检查系统版本  ######"
	if [[ -f /etc/redhat-release ]]; then
        	cat /etc/redhat-release
    	elif [[ -f /etc/lsb-release ]]; then
        	source /etc/lsb-release
        	echo $DISTRIB_DESCRIPTION
        else
        	echo "Unsupported system."
    	fi
}

function get_Cpu_Status(){
	echo ""
	echo "######  检查 CPU 状态  ######"
        Physical_CPUs=$(grep "physical id" /proc/cpuinfo| sort | uniq | wc -l)
        Virt_CPUs=$(grep "processor" /proc/cpuinfo | wc -l)
        CPU_Kernels=$(grep "cores" /proc/cpuinfo|uniq| awk -F ': ' '{print $2}')
        CPU_Type=$(grep "model name" /proc/cpuinfo | awk -F ': ' '{print $2}' | sort | uniq)
        CPU_Arch=$(uname -m)
        echo "物理CPU个数:$Physical_CPUs"
        echo "逻辑CPU个数:$Virt_CPUs"
        echo "每CPU核心数:$CPU_Kernels"
        echo "CPU型号:$CPU_Type"
        echo "CPU架构:$CPU_Arch"
}

function get_System_Status(){
	echo ""
	echo "######  检查系统状态  ######"
        if [ -e /etc/sysconfig/i18n ];then
		default_LANG="$(grep "LANG=" /etc/sysconfig/i18n | grep -v "^#" | awk -F '"' '{print $2}')"
        else
       		default_LANG=$LANG
        fi
	export LANG="en_US.UTF-8"
  	Kernel=$(uname -r)
  	OS=$(uname -o)
  	Hostname=$(uname -n)
  	LastReboot=$(who -b | awk '{print $3,$4}')
  	uptime=$(uptime|awk '{print $3,$4}')
  	xunihua=$(systemd-detect-virt)
  	echo "系统：$OS"
  	echo "内核：$Kernel"
  	echo "主机名：$Hostname"
  	echo "语言/编码：$default_LANG"
  	echo "当前时间：$(date +'%F %T')"
  	echo "最后启动：$LastReboot"
  	echo "运行时间：$uptime"
  	echo "虚拟化平台：$xunihua"
}

function get_Memory(){
	echo ""
	echo "######  检查内存空间  ######"
	mem_used=$(free | awk 'NR==2{printf "%.2f\n", $3/$2*100}')
	mem_all=$(free -h |awk 'NR==2{print $2}')
	echo "总内存:  $mem_all  内存使用率:  $mem_used%"
}

function get_Disk(){
	echo ""
	echo "######  检查磁盘空间  ######"
	lsblk -b -i -o NAME,MOUNTPOINT,SIZE,RO | awk '$NF==0' | while read line
	do
    		name=$(echo $line | awk '{print $1}')
    		mount_point=$(echo $line | awk '{print $2}'|grep -v boot)
    		size=$(echo $line | awk '{print $3}')
    		export_size=$(awk 'BEGIN{printf "%.2fG\n",'$size'/1024/1024/1024}')
    		if [[ $mount_point =~ ^/.*$ ]]; then
        		used=$(df -B 1 | awk -v mnt="$mount_point" '$NF==mnt{print $3}')
        		used_percent=$(awk 'BEGIN{printf "%.2f%\n",('$used'/'$size')*100}')
        		echo -e "挂载点: $mount_point\t总大小: $export_size\t使用率: $used_percent"
    		fi
	done
}

function get_Container(){
	echo ""
	echo "######  检查容器资源  ######"
	docker stats --no-stream |grep -v ID | awk '{print "容器名称: "$2,"\t""CPU使用率: "$3,"\t""内存使用率: "$7,"\t""内存使用: "$4,"\t""内存分配: "$6}'
	echo "------"
	container_list=$(docker ps --format "{{.Names}}")

	for container_name in $container_list
	do
  		start_time=$(docker inspect --format '{{ .State.StartedAt }}' $container_name)
  		start_timestamp=$(date -d "$start_time" +%s)
  		now=$(date +%s)
  		runtime=$((now - start_timestamp))

  		days=$((runtime/86400))
  		hours=$((runtime/3600%24))
  		minutes=$((runtime/60%60))
  		seconds=$((runtime%60))

  		echo "容器 $container_name 的运行时长为：$days 天 $hours 小时 $minutes 分钟 $seconds 秒"
	done
	echo "------"

	Check_Not_Run_num=$(docker ps -a --format "{{.ID}} {{.Names}} {{.Status}}"|grep Exited|wc -l)	
	if [ $Check_Not_Run_num = "0" ]; then
		echo "不存在停止状态的容器"
	else
		echo "存在停止运行的容器:"
		for Not_Run_ID in $(docker ps -a --format "{{.ID}} {{.Names}} {{.Status}}"|grep Exited|awk '{print $1}')
		do
			Not_Run_Name=$(docker ps -a --format "{{.ID}} {{.Names}}"|grep $Not_Run_ID|awk '{print $2}')			
			echo -e "容器名: $Not_Run_Name \t 容器ID: $Not_Run_ID"
		done
	fi
}

function get_DE_Version(){
	echo ""
	echo "######  检查 DataEase 版本  ######"
	dectl version
}

function get_Install_Mode(){
	echo ""
	echo "######  检查 DataEase 安装模式  ######"
	echo "安装模式:  `grep 'DE_ENGINE_MODE=' $Install_Folder/dataease/.env|awk  -F '=' '{print $2}'`"
	MySQL_External=$(grep 'DE_EXTERNAL_MYSQL=' $Install_Folder/dataease/.env |awk -F '=' '{print $2}')
	DORIS_External=$(grep 'DE_EXTERNAL_DORIS=' $Install_Folder/dataease/.env |awk -F '=' '{print $2}')
	if [ $DORIS_External = true ]; then
		echo ">>> Doris 外置部署"
	fi
	if [ $MySQL_External = true ]; then
		echo ">>> MySQL 外置部署"	
	fi
}


function get_Panel(){
	Panel_num=`$Login_Mysql "select count(*) all_panel_num from panel_group where node_type='panel';" 2>&1|grep -v "Using a password on the command line interface can be insecure"|grep [0-9] | awk -F '|' '{print $2}'|awk '{print $NF}'`
	echo ""
	echo  "######  仪表板数量  ######"
	echo "仪表板数量:  " $Panel_num

}

function get_Dataset(){
	echo ""
	echo "######  数据集数量  ######"
	$Login_Mysql "select type,count(type) from dataset_table GROUP BY type order by count(type) desc;" 2>&1|grep -v "Using a password on the command line interface can be insecure"|grep -v Warning |awk -v OFS='\t' -F "|" 'NR>3{gsub(/^[ \t]+|[ \t]+$/, "", $1); print $2,$3}'|head -n -1 |awk '{print "数据集: "$1,"\t""数量: "$2}'
}

function get_DataSource(){
	echo ""
	echo "######  数据源数量  ######"
	$Login_Mysql "select type datasource,COUNT(type) dataeource_number from datasource GROUP BY type order by COUNT(type) desc;" 2>&1|grep -v "Using a password on the command line interface can be insecure"|grep -v Warning | awk -v OFS='\t' -F "|" 'NR>3{gsub(/^[ \t]+|[ \t]+$/, "", $1); print $2,$3}'|head -n -1 |awk '{print "数据源: "$1,"\t""数量: "$2}'
}

function get_Error_DataSource(){
        echo ""
        echo "######  异常状态的数据源  ######"
	$Login_Mysql "select name 数据源,type 类型,create_by 创建人 from datasource where status != 'Success'" 2>&1|grep -v "Using a password on the command line interface can be insecure"|grep "|"|awk -F '|' '{print $2,$3,$4}'

}

function get_Panel_Users(){
	echo ""
	echo "######  创建仪表板用户数  ######"
	echo  "创建仪表板用户数:  " `$Login_Mysql "select COUNT(DISTINCT(create_by)) create_panel_users from panel_group where node_type='panel';" 2>&1|grep -v "Using a password on the command line interface can be insecure"|grep [0-9]|awk -F '|' '{print $2}'|awk '{print $NF}'`
}

function get_Dataset_Users(){
	echo ""
	echo "######  创建数据集用户数  ######"
	echo "创建数据集用户数:  " `$Login_Mysql "select COUNT(DISTINCT(create_by)) create_dataset_users from dataset_table;" 2>&1|grep -v "Using a password on the command line interface can be insecure"|grep [0-9]|awk -F '|' '{print $2}'|awk '{print $NF}'`
}

function get_chart_number(){
        echo ""
        echo "######  按用户统计视图数量  ######"
	echo "创建用户      视图总数"
	echo "`$Login_Mysql "select create_by, count(*)  from chart_view GROUP BY create_by;" 2>&1|grep -v "Using a password on the command line interface can be insecure"|grep -v create_by |grep '|'|awk -F '|' '{print $2,$3}'|sort -rnk +2`"

        echo ""
	echo "######  按视图类型统计视图数量  ######"
	echo "`$Login_Mysql "SELECT  type 视图类型,render 视图来源,count(*) 数量 FROM  chart_view GROUP BY type,render order by count(*) desc;" 2>&1|grep -v "Using a password on the command line interface can be insecure"|grep '|'|awk -F '|' '{print $2,$3,$4}'`"
}

function get_Dataset_Task(){
	echo ""
	echo "######  定时同步任务数  ######"
	echo "进行中的定时同步任务数:  "`$Login_Mysql "select count(*) from dataset_table_task where status='Underway'" 2>&1|grep -v "Using a password on the command line interface can be insecure"|grep [0-9]|awk -F '|' '{print $2}'|awk '{print $NF}'`
	echo "------"
	echo "同步报错的任务数:  "`$Login_Mysql "select count(*) from dataset_table_task where last_exec_status='Error' and status='Underway'" 2>&1|grep -v "Using a password on the command line interface can be insecure"|grep [0-9]|awk -F '|' '{print $2}'|awk '{print $NF}'`
	echo "------"
	 $Login_Mysql "select name 数据集任务,substr(FROM_UNIXTIME(last_exec_time/1000),1,19) 上次同步时间,(case type when 'add_scope' then '增量更新' when 'all_scope' then '全量更新' end ) 同步类型 from dataset_table_task where last_exec_status='Error' and status='Underway' ORDER BY 上次同步时间 desc"  2>&1|grep -v "Using a password on the command line interface can be insecure"|grep "|"|awk -F '|' '{print $2,$3,$4}'
}

function get_UsersType(){
        echo ""
        echo "######  用户统计  ######"
	echo "用户总数:  " `$Login_Mysql "select COUNT(username) user_number from sys_user;" 2>&1|grep -v "Using a password on the command line interface can be insecure"|grep [0-9]|awk -F '|' '{print $2}'|awk '{print $NF}'`
	echo "------"
	$Login_Mysql 'select (case `from` when 0 then "DataEase" when 1 then "LDAP" when 2 then "OIDC" when 3 then "CAS" when 4 then "企业微信" when 5 then "钉钉" when 6 then "飞书" end) 用户来源,count(*) 用户数 from sys_user GROUP BY `from`' 2>&1|grep -v "Using a password on the command line interface can be insecure"|grep "|"|awk -F '|' '{print $2,$3}'
}

function get_Roles(){
	echo ""
	echo "######  角色数量  ######"
	echo "角色数量:  " `$Login_Mysql "select COUNT(name) role_number from sys_role;" 2>&1|grep -v "Using a password on the command line interface can be insecure"|grep [0-9]|awk -F '|' '{print $2}'|awk '{print $NF}'`
}

function get_Organization (){
	echo ""
	echo "######  组织数量  ######"
	echo "组织节点数:  " `$Login_Mysql "select count(*) from sys_dept" 2>&1|grep -v "Using a password on the command line interface can be insecure"|grep [0-9]|awk -F '|' '{print $2}'|awk '{print $NF}'`
}

function get_Scheduled_Reports(){
	echo ""
	echo "######  定时报告数  ######"
	echo "定时报告数量:  " `$Login_Mysql "select COUNT(task_name)  from sys_task where rate_type;" 2>&1|grep -v "Using a password on the command line interface can be insecure"|grep [0-9]|awk -F '|' '{print $2}'|awk '{print $NF}'`
	echo "------"
	echo "定时报告失败任务:"
	$Login_Mysql "SELECT t.task_name 任务名, substr(FROM_UNIXTIME(i.execute_time/1000),1,19) AS 最近执行时间, if(i.status='-1','执行失败','执行成功') 执行状态,i.info 失败详情 FROM sys_task t LEFT JOIN ( SELECT task_id, MAX(execute_time) AS execute_time FROM sys_task_instance GROUP BY task_id) latest ON t.task_id = latest.task_id LEFT JOIN sys_task_instance i ON latest.task_id = i.task_id AND latest.execute_time = i.execute_time WHERE t.status = 1 and  i.status = '-1' ORDER BY t.task_id;" 2>&1|grep -v "Using a password on the command line interface can be insecure"|grep '|' |awk -F '|' '{print $2,$2,$4,$5}'
}

function get_View_Panel_30day(){
	echo ""
	echo "######  近 30 日查看仪表板  ######"
	echo "查看仪表板数:  " `$Login_Mysql "SELECT count(distinct(source_name)) FROM sys_log WHERE time >= UNIX_TIMESTAMP(DATE_SUB(CURDATE(), INTERVAL 30 DAY)) * 1000 and  source_type in (3,5) and operate_type in (13,14)" 2>&1|grep -v "Using a password on the command line interface can be insecure"|grep [0-9]|awk -F '|' '{print $2}'|awk '{print $NF}' `
	echo "------"
	echo "仪表板查看次数:  " `$Login_Mysql  "SELECT count(*) 访问次数 FROM sys_log WHERE time >= UNIX_TIMESTAMP(DATE_SUB(CURDATE(), INTERVAL 30 DAY)) * 1000 and  source_type in (3,5) and operate_type in (13,14)" 2>&1|grep -v "Using a password on the command line interface can be insecure"|grep [0-9]|awk -F '|' '{print $2}'|awk '{print $NF}'`
}

function get_Login_System_30day(){
	echo ""
	echo "######  近 30 日登录系统次数  ######"
	echo "近30日登录系统次数:  " `$Login_Mysql "select count(*) AS 登录次数 from sys_log where operate_type = 12 and substr( from_unixtime( time / 1000 ), 1, 10 )  >= DATE_SUB(NOW(), INTERVAL 30 DAY); " 2>&1|grep -v "Using a password on the command line interface can be insecure"|grep "|"|tail -1|awk '{print $2}'`
}

function get_Add_DataSource_30day(){
        echo ""
        echo "######  近 30 日新增数据源  ######"
        echo "新增数据源数量:  " `$Login_Mysql "select count(*) 数量 from datasource where create_time >= UNIX_TIMESTAMP(DATE_SUB(CURDATE(), INTERVAL 30 DAY)) * 1000"  2>&1|grep -v "Using a password on the command line interface can be insecure"|grep [0-9] |awk -F '|' '{print $2}'|awk '{print $NF}'`
	echo "------"
	$Login_Mysql "select type 数据源类型, count(name) 数量 from datasource where create_time >= UNIX_TIMESTAMP(DATE_SUB(CURDATE(), INTERVAL 30 DAY)) * 1000 GROUP BY type"  2>&1|grep -v "Using a password on the command line interface can be insecure"|grep '|'|awk -F '|' '{print $2,$3}'
}

function get_Add_Dataset_30day(){
        echo ""
        echo "######  近 30 日新增数据集  ######"
	echo "新增数据集数量:  " `$Login_Mysql "select count(*) from dataset_table where create_time >= UNIX_TIMESTAMP(DATE_SUB(CURDATE(), INTERVAL 30 DAY)) * 1000 " 2>&1|grep -v "Using a password on the command line interface can be insecure"|grep [0-9] |awk '{print $2}'|awk '{print $NF}'`
	echo "------"
	$Login_Mysql "select type 数据集类型,count(name) 数量 from dataset_table where create_time >= UNIX_TIMESTAMP(DATE_SUB(CURDATE(), INTERVAL 30 DAY)) * 1000 GROUP BY type order by count(*) desc"  2>&1|grep -v "Using a password on the command line interface can be insecure"|grep '|'|awk -F '|' '{print $2,$3}'
}

function get_Add_Panel_30day(){
        echo ""
        echo "######  近 30 日新增仪表板  ######"
	echo "新增仪表板:  " `$Login_Mysql "select count(*) from panel_group where node_type = 'panel' and create_time >= UNIX_TIMESTAMP(DATE_SUB(CURDATE(), INTERVAL 30 DAY)) * 1000" 2>&1|grep -v "Using a password on the command line interface can be insecure"|grep [0-9]|awk -F '|' '{print $2}'|awk '{print $NF}'`
}

function get_Edit_Panel_30day(){
        echo ""
        echo "######  近 30 日编辑仪表板  ######"
	echo "编辑仪表板数量:  " `$Login_Mysql "select count(*) from panel_group where node_type = 'panel' and update_time >= UNIX_TIMESTAMP(DATE_SUB(CURDATE(), INTERVAL 30 DAY)) * 1000 " 2>&1|grep -v "Using a password on the command line interface can be insecure"|grep [0-9] |awk -F '|' '{print $2}'|awk '{print $NF}'`
}

function get_Doris_Data(){
	Install_Mode=$(grep 'DE_ENGINE_MODE=' $Install_Folder/dataease/.env|awk  -F '=' '{print $2}')
	if [ "$Install_Mode" = "simple" ]; then
		echo ""
	else		
		echo ""
		echo "######  统计 Doris 数据量  ######"
		echo "Doris 数据存储量:  " `$Login_Doris "show data;" 2>&1|grep -v "Using a password on the command line interface can be insecure"|grep Total|awk -F '|' '{print $3}'`
			mem_limit=`$Login_Doris 'show variables like "%mem_limit%";' 2>&1|grep -v "Using a password on the command line interface can be insecure"|grep "exec_mem_limit"|awk '{print $4}'`
			mem_limit_size=$(awk 'BEGIN{printf "%.2fG\n",'$mem_limit'/1024/1024/1024}')

		all_data_row=0;
		table_num=0;
		for i in `$Login_Doris "show data;" 2>&1|grep -v "Using a password on the command line interface can be insecure"|grep ds_*|awk -F '|' '{print $2}' `;
		do
				data_row=$($Login_Doris "show data from $i" 2>&1|grep -v "Using a password on the command line interface can be insecure"|grep ds_*|awk -F '|' '{print $6}');
				let all_data_row+=data_row;
				let table_num+=1;
		done
		echo "Doris 表总数:  " $table_num
		echo "Doris 库总数据行:  " $all_data_row
		echo "mem_limit 配置:  " $mem_limit_size
	fi
}

function get_doris_Maxdata_Table(){
	Install_Mode=$(grep 'DE_ENGINE_MODE=' $Install_Folder/dataease/.env|awk  -F '=' '{print $2}')
	if [ "$Install_Mode" = "simple" ]; then
		echo ""
	else		
		for table in $($Login_Doris "show tables;" 2>&1|grep -v "Using a password on the command line interface can be insecure"|grep 'ds_'|awk -F '|' '{print $2}'|awk '{print $NF}')
		do
				rows=$($Login_Doris "select count(*) from $table" 2>&1|grep -v "Using a password on the command line interface can be insecure"|grep [0-9]|awk -F '|' '{print $2}'|awk '{print $NF}')
				echo "$rows" >> /tmp/table_rows
		done
		echo  "Doris 最大的表行数:  `cat /tmp/table_rows |sort -rnk1  | head -n1`"
		echo > /tmp/table_rows
	fi
}

function Check_Backup(){
	echo ""
	echo "######  检查是否配置备份  ######"
	yesterday=$(date -d "yesterday" +%Y_%m_%d)
	today=$(date +%Y_%m_%d)

	crontab -l|grep backup-de.sh >/dev/null 2>&1
	present_cron=$(echo $?)
	if [ "$present_cron" = "0" ]; then
		echo "crontab 存在备份任务: `crontab -l|grep backup-de.sh`"
		backup_file=`crontab -l | grep "backup-de.sh" | sed 's/.* \([^ ]*\)$/\1/'`
		if [ -f "$backup_file" ]; then
			echo "存在备份脚本: $backup_file"
			if [ -n "$(find /opt/bak -maxdepth 1 -name "dataease_${yesterday}*")" -o -n "$(find /opt/bak -maxdepth 1 -name "dataease_${today}*")" ]; then
			echo "备份正常进行中，最近的备份文件是：$(ls -lrt /opt/bak/dataease_* |grep -v "总用量"|awk '{print $9}'|tail -1)"
			else	
				echo "近两天没有备份文件！"
			fi
		else 
			echo "crontab 未检测到备份脚本！"		
		fi
	else 
		echo "crontab 未配置备份任务！"
	fi
}

function Check_Binlog(){
        echo ""
        echo "######  检查 MySQL Binlog 是否开启  ######"
	Binlog_Status=`$Login_Mysql "show variables like 'log_%';" 2>&1|grep -v "Using a password on the command line interface can be insecure"|grep -w "log_bin"|awk -F '|' '{print $3}'`
	if [ $Binlog_Status == "ON" ]; then
		Binlog_Dir=`$Login_Mysql "show variables like 'log_%';" 2>&1|grep -v "Using a password on the command line interface can be insecure"|grep -w "log_bin_basename"|awk -F '|' '{print $3}'`
		Binlog_days=`$Login_Mysql "show variables like 'expire_logs_days';" 2>&1|grep -v "Using a password on the command line interface can be insecure"|grep -w "expire_logs_days"|awk -F '|' '{print $3}'|awk '{print $NF}'`
		FILE_SIZES=`$Login_Mysql "SHOW BINARY LOGS;" 2>&1|grep -v "Using a password on the command line interface can be insecure"|grep [0-9]|awk -F '|' '{print $3}'|awk '{print $NF}'`
		TOTAL_SIZE=0
		for FILE_SIZE in $FILE_SIZES
		do
			let TOTAL_SIZE+=FILE_SIZE
		done
		TOTAL_SIZE=$(awk 'BEGIN{printf "%.2f\n",'$TOTAL_SIZE'/1024/1024}')
		echo "Binlog 已开启"
		echo "Binlog 位置: $Binlog_Dir"
		echo "Binlog 保留时长: $Binlog_days 天"
		echo "Binlog 当前总大小: $TOTAL_SIZE MB"
	else
		echo "Binlog 未开启!"
	fi
}

[ $(id -u) -gt 0 ] && echo "请使用 root 用户运行该脚本! " && exit 1

echo "正在执行巡检脚本..."

Install_Folder=$(grep "DE_BASE=" /usr/bin/dectl |awk -F '=' '{print $2}')
MySQL_Host=$(grep "DE_MYSQL_HOST=" $Install_Folder/dataease/.env|awk  -F '=' '{print $2}')
MySQL_User=$(grep "DE_MYSQL_USER=" $Install_Folder/dataease/.env|awk  -F '=' '{print $2}')
MySQL_Password=$(grep "DE_MYSQL_PASSWORD=" $Install_Folder/dataease/.env|awk  -F '=' '{print $2}')
MySQL_DB=$(grep "DE_MYSQL_DB=" $Install_Folder/dataease/.env|awk  -F '=' '{print $2}')
MySQL_Port=$(grep "DE_MYSQL_PORT=" $Install_Folder/dataease/.env|awk  -F '=' '{print $2}')
Doris_Host=$(grep "DE_DORIS_HOST=" $Install_Folder/dataease/.env|awk  -F '=' '{print $2}')
Doris_User=$(grep "DE_DORIS_USER=" $Install_Folder/dataease/.env|awk  -F '=' '{print $2}')
Doris_Password=$(grep "DE_DORIS_PASSWORD=" $Install_Folder/dataease/.env|awk  -F '=' '{print $2}')
Doris_DB=$(grep "DE_DORIS_DB=" $Install_Folder/dataease/.env|awk  -F '=' '{print $2}')
Doris_Port=$(grep "DE_DORIS_PORT=" $Install_Folder/dataease/.env|awk  -F '=' '{print $2}')

sysctl -w net.bridge.bridge-nf-call-iptables=1 >/dev/null
sysctl -w net.bridge.bridge-nf-call-ip6tables=1 >/dev/null
DE_MySQL_Name=$(dectl status|grep mysql|awk '{print $1}')

if [ "$MySQL_Host" = "$DE_MySQL_Name" ];then
	Login_Mysql="docker exec  $MySQL_Host mysql -u$MySQL_User -p$MySQL_Password $MySQL_DB -t -e"
	Login_Doris="docker exec  $MySQL_Host mysql -u$Doris_User -p$Doris_Password -h$Doris_Host -P$Doris_Port  $Doris_DB -A -t -e"
else	
	Login_Mysql="./tools/mysql -u$MySQL_User -p$MySQL_Password -h$MySQL_Host -P$MySQL_Port $MySQL_DB -t -e"
	Login_Doris="./tools/mysql -u$Doris_User -p$Doris_Password -h$Doris_Host -P$Doris_Port $Doris_DB -A -t -e"

fi


RESULTFILE="./DataEaseCheck-`date +%Y%m%d`.txt"

check(){
Total=0
Set=3
	get_OS_Version > $RESULTFILE
		((Total+=Set))
		printf "已完成：$Total %%\r" 
	get_Cpu_Status >> $RESULTFILE
		((Total+=Set))
		printf "已完成：$Total %%\r" 
	get_System_Status  >> $RESULTFILE
		((Total+=Set))
		printf "已完成：$Total %%\r" 
	get_Memory  >> $RESULTFILE
		((Total+=Set))
		printf "已完成：$Total %%\r" 
	get_Disk  >> $RESULTFILE
		((Total+=Set))
		printf "已完成：$Total %%\r" 	 
	get_DE_Version  >> $RESULTFILE
		((Total+=Set))
		printf "已完成：$Total %%\r" 
	get_Install_Mode  >> $RESULTFILE
		((Total+=Set))
		printf "已完成：$Total %%\r" 
	get_Container  >> $RESULTFILE
		((Total+=Set))
		printf "已完成：$Total %%\r"
	get_Panel >> $RESULTFILE
		((Total+=Set))
		printf "已完成：$Total %%\r" 
	get_Dataset  >> $RESULTFILE
		((Total+=Set))
		printf "已完成：$Total %%\r" 
	get_Dataset_Task  >> $RESULTFILE
		((Total+=Set))
		printf "已完成：$Total %%\r" 
	get_DataSource  >> $RESULTFILE
		((Total+=Set))
		printf "已完成：$Total %%\r" 
	get_Error_DataSource  >> $RESULTFILE
		((Total+=Set))
		printf "已完成：$Total %%\r" 
	get_Panel_Users  >> $RESULTFILE
		((Total+=Set))
		printf "已完成：$Total %%\r" 
	get_Dataset_Users >> $RESULTFILE
		((Total+=Set))
		printf "已完成：$Total %%\r" 
	get_UsersType  >> $RESULTFILE
		((Total+=Set))
		printf "已完成：$Total %%\r" 
	get_Roles  >> $RESULTFILE
		((Total+=Set))
		printf "已完成：$Total %%\r" 
	get_Organization  >> $RESULTFILE
		((Total+=Set))
		printf "已完成：$Total %%\r" 
	get_Scheduled_Reports >> $RESULTFILE
		((Total+=Set))
		printf "已完成：$Total %%\r" 
	get_chart_number  >> $RESULTFILE
		((Total+=Set))
		printf "已完成：$Total %%\r" 
	get_View_Panel_30day  >> $RESULTFILE
		((Total+=Set))
		printf "已完成：$Total %%\r" 
	get_Login_System_30day  >> $RESULTFILE
		((Total+=Set))
		printf "已完成：$Total %%\r" 
	get_Add_DataSource_30day >> $RESULTFILE
		((Total+=Set))
		printf "已完成：$Total %%\r" 
	get_Add_Dataset_30day  >> $RESULTFILE
		((Total+=Set))
		printf "已完成：$Total %%\r" 
	get_Add_Panel_30day  >> $RESULTFILE
		((Total+=Set))
		printf "已完成：$Total %%\r" 
	get_Edit_Panel_30day   >> $RESULTFILE
		((Total+=Set))
		printf "已完成：$Total %%\r" 
	get_Doris_Data >> $RESULTFILE
		((Total+=Set))
		printf "已完成：$Total %%\r" 
	get_doris_Maxdata_Table   >> $RESULTFILE
		((Total+=Set))
		printf "已完成：$Total %%\r" 
	Check_Backup  >> $RESULTFILE
		((Total+=Set))
		printf "已完成：$Total %%\r" 
	Check_Binlog  >> $RESULTFILE
		printf "100 %%\r" 
}

check
echo "已完成：100 %"
Generate_CSV
echo "正在生成 CSV 文件 ..."
echo "检查结果保存于：$RESULTFILE"
